package com.asia.algorithmcode.array;

/**
 * @DESCRIPTION:
 * @USER: wanfu
 * @DATE: 2025/3/12 星期三 18:42
 */
public class PlusOne {


    public static void main(String[] args) {
        int[] arr = new int[]{9,9,8,9,9};
        int[] ints = new PlusOne().plusOne(arr);
        for (int anInt : ints) {
            System.out.printf(anInt + "\t");
        }
    }

    public int[] plusOne(int[] digits) {
        int n = digits.length;
        int add = 0;
        for (int i = n - 1; i >= 0; i--) {
            // 最后一位能加上
            if (digits[i] < 9) {
                digits[i] = digits[i] + 1;
                return digits;
            } else {
                add = 1;
            }

            int res = digits[i] + 1;
            if (res == 10) {
                digits[i] = 0;
            } else {
                digits[i] = res;
                add = 0;
            }

        }
        if (add == 1) {
            int[] arr = new int[n + 1];
            arr[0] = 1;
            return arr;
        } else {
            return digits;
        }
    }

}
